import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
// import 'package:fluro_fork/fluro_fork.dart';

import 'package:hkzf/pages/home/index.dart';
import 'package:hkzf/pages/login.dart';
import 'package:hkzf/pages/room_add/index.dart';
import 'package:hkzf/pages/room_detail/index.dart';
import 'package:hkzf/pages/setting.dart';
import 'package:hkzf/pages/room_manage/index.dart';
import 'package:hkzf/pages/user_center/index.dart';

import 'pages/not_found.dart';
import 'pages/register.dart';

class Routes {
  static String home = '/';
  static String login = '/login';
  static String roomDetail = '/room/:roomId';
  static String register = '/register';
  static String setting = '/setting';
  static String roomManage = '/roomManage';
  static String roomAdd = '/roomAdd';
  static String userCenter = '/usercenter';
  static Handler homeHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return HomePage();
  });
  static Handler loginHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return LoginPage();
  });
  static Handler registerHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return RegisterPage();
  });
  static Handler roomDetailHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return RoomDetailPage(
      roomId: params['roomId'][0],
    );
  });
  static Handler settingHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return SettingPage();
  });
  static Handler roomManageHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return RoomManage();
  });
  static Handler roomAddHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return RoomAddPage();
  });
  static Handler userCenterHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return UserCenter();
  });
  static Handler notFoundHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return NotFoundPage();
  });

  // 路由菜单
  static Map<String, Handler> routeHanderMap = {
    home: homeHandler,
    login: loginHandler,
    roomDetail: roomDetailHandler,
    register: registerHandler,
    setting: settingHandler,
    roomManage: roomManageHandler,
    roomAdd: roomAddHandler,
    userCenter: userCenterHandler,
  };

  // 无需登录白名单
  static List<String> notNeedLogin = [
    home,
    login,
    register,
    setting,
    roomDetail
  ];

  // 拦截器 如果返回一个函数就结束跳转，其他值则继续跳转
  // 需要修改fluro依赖代码，难以支持async，目前取消使用
  static String interceptor(AppRoute route) {
    // if(route.route)
    // print(route.route);
    // print(notNeedLogin);
    // if (notNeedLogin.indexOf(route.route) == -1) {
    //   return '/login' + route.route;
    // }

    return null;
  }

  static void configureRoutes(Router router) {
    // router.define(home, handler: homeHandler);
    // router.define(login, handler: loginHandler);
    // router.define(roomDetail, handler: roomDetailHandler);
    // router.define(register, handler: registerHandler);
    // router.define(setting, handler: settingHandler);
    // router.define(roomManage, handler: roomManageHandler);
    // router.define(roomAdd, handler: roomAddHandler);
    // 注册路由
    routeHanderMap.forEach((String name, Handler handler) {
      // 增加拦截器,如果路由自带拦截器，则不使用公共拦截器
      // Handler _handler = Handler(
      //     type: handler.type,
      //     handlerFunc: handler.handlerFunc,
      //     // interceptor: interceptor
      //     );
      router.define(name, handler: handler);
    });

    router.notFoundHandler = notFoundHandler;
  }
}
